package org.itboys.mobile.service.mongo.weixin;

import org.itboys.framework.resource.ResourceHolder;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.floorMr.SundayFloorDetailDto;
import org.itboys.mobile.dto.floorMr.SundayFloorMrGroupDto;
import org.itboys.mobile.entity.mongo.SundayFloorMrGroup;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/7/18
 * @Description：小程序楼先生
 */
@Service
public class SundayWeiXInFloorMrGroupService extends BaseMongoService<SundayFloorMrGroup>{
    @Resource(name = "mobileDs")
    private MongoDataSource yefuDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return yefuDataSource;
    }

    @Override
    protected Class<SundayFloorMrGroup> getEntityClass() {
        return SundayFloorMrGroup.class;
    }
    @Autowired
    private ResourceHolder resourceHolder;
    /**
     * 分页获取楼先生
     * @param request
     * @return
     */
    public List<SundayFloorMrGroupDto> select(HttpServletRequest request){

        Map<String,Object> param= ParamUtil.packageMongoExactParam(request);
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("sort","ct");
        param.put("order","desc");
        List<SundayFloorMrGroup> groups = super.pageQuery(request,param).getData();
        return  packageGroupDto(groups);
    }

    public List<SundayFloorMrGroupDto> packageGroupDto( List<SundayFloorMrGroup> groups){
        List<SundayFloorMrGroupDto> result=new ArrayList<>();
        for (SundayFloorMrGroup group:groups) {
            SundayFloorMrGroupDto groupDto=new SundayFloorMrGroupDto();
            groupDto.setId(group.getId());
            groupDto.setSubTitle(group.getSubTitle());
            groupDto.setTitleName(group.getTitle());
            groupDto.setImage(resourceHolder.getStringValue("imgRoot")+group.getImage());
            result.add(groupDto);
        }
        return result;
    }

    public SundayFloorDetailDto getDetail(Long id){
        SundayFloorMrGroup floorMrGroup = super.getById(id);
        SundayFloorDetailDto result=new SundayFloorDetailDto();
        if(floorMrGroup!=null){
            result.setTitle(floorMrGroup.getTitle());
            result.setLink(floorMrGroup.getLink());
        }
        return result;
    }
}
